import {
	baseUrl
} from '@/static/utils/env.js'
import {
	Loading,
	Routes,
	Toast
} from '@/static/utils/utils.js'
//简单封装请求方式
const request = (url, method = {}, data = {}, loadtxt = '', header = {}) => {
	url = `${baseUrl}/${url}`
	method = method.toUpperCase()
	header.token = uni.getStorageSync('token')
	if (loadtxt) {
		Loading.show(loadtxt)
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			method,
			header,
			data,
			dataType: 'json',
			responseType: 'text',
			success: response => {
				if (response.data.code === -1 || response.data.code === 0 && response.data.msg.includes('token')) {
					Routes.lunch('/pages/login/login')
					return
				}
				resolve(response.data)
			},
			fail: error => {
				reject(error)
			},
			complete() {
				Loading.hidden()
			}
		})
	})
}
export function get(url, data, loadtxt, header) {
	return request(url, 'GET', data, loadtxt, header)
}

export function post(url, data, loadtxt, header) {
	return request(url, 'POST', data, loadtxt, header)
}